<template>
  <div>
    <h2>留言列表：</h2>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="_id" label="留言ID" align="center">
      </el-table-column>
      <el-table-column label="文章标题" align="center">
        <template slot-scope="{ row }">
          <div v-if="row.Aid != null">{{ row.Aid.title }}</div>
          <div v-else>文章已被删除</div>
        </template>
      </el-table-column>
      <el-table-column label="留言人" align="center">
        <template slot-scope="{ row }">
          <div v-if="row.Uid != null">
            {{ row.Uid.account }}
          </div>
          <div v-else>用户已被删除</div>
        </template>
      </el-table-column>
      <el-table-column label="留言内容" align="center">
        <template slot-scope="{ row }">
          <div>
            {{ row.content }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="留言回复" align="center">
        <template slot-scope="{ row }">
          <div>
            <el-tag :color="$randomColor()">{{
              row.lists ? row.lists[0].content : "未回复"
            }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="created_time" label="留言时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" @click="open(row)">回复</el-button>
          <el-button type="text" @click="deleteComment(row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="回复" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-form :model="form" :rules="rulesReply" ref="refReply">
          <el-form-item label="留言回复" prop="replys">
            <el-input v-model="form.replys"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="reply">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCommentlist, getCommentdelete, getCommentup } from "@/api/index.js";
export default {
  data() {
    return {
      tableData: [],
      loading: true,
      dialogVisible: false,
      items: {},
      form: {},
      rulesReply: {
        replys: [
          { required: true, message: "请输入回复内容", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getmessagelist();
  },
  methods: {
    getmessagelist() {
      getCommentlist().then((res) => {
        this.tableData = res.data.items;
        this.loading = false;
      });
    },
    open(val) {
      this.dialogVisible = true;
      this.items = val;
    },
    // 回复
    reply() {
      this.$refs.refReply.validate((valid) => {
        if (valid) {
          let models = {
            Aid: this.items.Aid._id,
            Uid: JSON.parse(localStorage.user)._id,
            To: this.items.Uid.account,
            parentId: this.items._id,
            content: this.form.replys,
          };
          getCommentup(models).then((res) => {
            if (res.status == 200) {
              this.$message.success("回复成功！");
              this.$refs.refReply.resetFields();
              this.getmessagelist();
              this.dialogVisible = false;
            }
          });
        }
      });
    },
    deleteComment(id) {
      getCommentdelete(id).then((res) => {
        if (res.data.status == 200) {
          this.$message.success(res.data.msg);
          this.getmessagelist();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
